<template>
  <div class="user-profile">
    <van-nav-bar
      class="page-nav-bar"
      title="个人信息"
      left-arrow 
      @click-left="$router.back()" 
    />

    <input type="file" hidden ref="inputFile" @change="inputChange">
    <van-cell title="头像" is-link @click="$refs.inputFile.click()">
      <van-image
        class="avatar"
        fit="cover"
        round
        :src="user.photo"
      />
    </van-cell>

    <van-cell title="昵称"
      :value="user.name" 
      @click="isShowUpdateName = true"
      is-link 
    />
    <van-cell
      title="性别" 
      :value="user.gender === 0 ? '男' : '女'" 
      is-link 
      @click="isShowUpdateGender = true"
    />
    <van-cell 
      title="生日" 
      :value="user.birthday || '1977-00-00'" 
      is-link 
      @click="isShowUpdateBirthdy = true"
    />

    <van-popup
      v-model="isShowUpdateName"
      style="height: 100%"
      position="bottom">
      <update-name 
        v-if="isShowUpdateName"
        v-model="user.name"
        @close="isShowUpdateName=false"
      />
    </van-popup>

    <van-popup
      v-model="isShowUpdateGender"
      position="bottom">
      <update-gender 
        @close="isShowUpdateGender = false" 
        v-if="isShowUpdateGender"
        v-model="user.gender"
      />
    </van-popup>

    <van-popup
      v-model="isShowUpdateBirthdy"
      position="bottom"
    >
      <update-birthdy
        v-if="isShowUpdateBirthdy" 
        @close="isShowUpdateBirthdy=false"
        v-model="user.birthday"
      />
    </van-popup>

    <van-popup
      v-model="isShowUpdatePhoto"
      style="height: 100%;"
      position="bottom"
    >
      <update-photo
        v-if="isShowUpdatePhoto" 
        :imgUrl="imgUrl"
        @close="isShowUpdatePhoto=false"
        @updatePhoto="user.photo = $event"
      />
    </van-popup>
  </div>

</template>
<script>
import {getUserProfile} from '@/api/user'
import UpdateName from './components/update-name'
import UpdateGender from './components/update-gender'
import UpdateBirthdy from './components/update-birthdy'
import UpdatePhoto from './components/update-photo'

export default {
  data() {
    return {
      user: {},
      isShowUpdateName: false,
      isShowUpdateGender: false,
      isShowUpdateBirthdy: false,
      isShowUpdatePhoto: false,
      imgUrl: null
    }
  },
  components: {
    UpdateName,
    UpdateGender,
    UpdateBirthdy,
    UpdatePhoto
  },
  created() {
    this.getUser()
  },
  methods: {
    async getUser() {
      try {
        const res = await getUserProfile()
        this.user = res.data.data
      } catch (err) {
        this.$toast('获取数据失败')
      }
    },
    inputChange() {
      const file = this.$refs.inputFile.files[0]
      this.imgUrl = window.URL.createObjectURL(file)

      this.isShowUpdatePhoto = true
      this.$refs.inputFile.value = ''
    }
  }
}
</script>

<style lang="less" scoped>
.user-profile {
  .avatar {
    width: 60px;
    height: 60px;
  }
  .van-popup{
    background: #f5f7f9;
  }
}
</style>
